package com.jiuwei.commons.mysql2word.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {
    public static DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    public static DateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");
    public static DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static String DAY_START_TIME = "00:00:00";
    public static String DAY_END_TIME = "23:59:59";
    public static final String FMT_YMD_HMS = "yyyy-MM-dd HH:mm:ss";
    public static final String FMT_YMD = "yyyy-MM-dd";

    public DateUtil() {
    }

    public static Date addMonthDate(Date date, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(2, month);
        return c.getTime();
    }

    public static Date addDayDate(Date date, int day) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(5, day);
        return c.getTime();
    }

    public static Date addHourDate(Date date, int hour) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(10, hour);
        return c.getTime();
    }

    public static Date addMinuteDate(Date date, int minute) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(12, minute);
        return c.getTime();
    }

    public static Date getDate(String timeStr, String fmt) {
        SimpleDateFormat format = new SimpleDateFormat(fmt);
        Date date = null;

        try {
            date = format.parse(timeStr);
        } catch (ParseException var5) {
            ;
        }

        return date;
    }

    public static String date2Str(Date date, String format) {
        if (date == null) {
            return "";
        } else {
            DateFormat dateFormat = new SimpleDateFormat(format);
            return dateFormat.format(date);
        }
    }

    public static String dayBeginStr(Date date) {
        return date2Str(date, "yyyy-MM-dd") + " 00:00:00";
    }

    public static String curDate() {
        Date curDate = new Date();
        return DATE_FORMAT.format(curDate);
    }

    public static String currentTime() {
        Date curDate = new Date();
        return TIME_FORMAT.format(curDate);
    }

    public static String currentDateTime() {
        Date curDate = new Date();
        return DATE_TIME_FORMAT.format(curDate);
    }

    public static byte dateCompare(String date1, String date2) throws ParseException {
        Date d1 = DATE_FORMAT.parse(date1);
        Date d2 = DATE_FORMAT.parse(date2);
        if (d1.before(d2)) {
            return -1;
        } else {
            return (byte)(d1.equals(d2) ? 0 : 1);
        }
    }

    public static Date str2date(String dateStr) throws ParseException {
        Date date = DATE_FORMAT.parse(dateStr);
        return date;
    }

    public static String date2str(Date date) throws ParseException {
        return date != null ? DATE_FORMAT.format(date) : "null";
    }

    public static String getCurrentWeek(int dayOfWeek) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        if (1 == dayOfWeek) {
            calendar.add(4, 1);
        }

        calendar.set(7, dayOfWeek);
        return DATE_FORMAT.format(calendar.getTime());
    }

    public static long dateMinus(Date date1, Date date2, char flag) {
        long msMinus = date1.getTime() - date2.getTime();
        switch(flag) {
            case 'M':
                return msMinus / 2592000000L;
            case 'd':
                return msMinus / 86400000L;
            case 'h':
                return msMinus / 3600000L;
            case 'm':
                return msMinus / 60000L;
            case 's':
                return msMinus / 1000L;
            case 'y':
                return msMinus / 31536000000L;
            default:
                return msMinus;
        }
    }

    public static Date datePlus(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(5, calendar.get(5) + days);
        return calendar.getTime();
    }
}
